####
#
# Copyright 2007 B. Chandler Carruth
#
# Licensed under the Apache License, Version 2
# .0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
####
#
# ./test/run_rcons_module.rb
#
####

# This is a utility to run RCons from a tester. It mangles the paths
# around, and manages arguments nicely. It yields a pipe if needed.
module RunRCons
  def run_rcons(*args)
    args.collect!() do |arg|
      arg = arg.to_s().strip()
      if arg =~ /\s/
        '"' + arg + '"'
      else
        arg
      end
    end

    path = File.dirname( __FILE__ )
    lib_path = File.join( path, '..', '..', 'lib' )
    bin_path = File.join( path, '..', '..', 'bin' )
    bin = File.join( bin_path, 'rcons' )

    # FIXME: This should not depend on "ruby" being in the path.
    IO.popen( "ruby -I#{lib_path} #{bin} #{args.join(' ')} 2>&1" ) do |p|
      if block_given?
        yield( p )
      end
    end

    return $? == 0
  end
end



